package com.example.springbootproject.service.impl;/*
 *@author 王志威
 *@datatime 2024/4/16 13:08
 *@editnote
 */

import com.example.springbootproject.mapper.RegisterMapper;
import com.example.springbootproject.pojo.Admin;
import com.example.springbootproject.service.RegisterService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.xml.bind.annotation.adapters.HexBinaryAdapter;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

@Service
@Slf4j
public class RegisterServiceImpl implements RegisterService {
    @Autowired
    private RegisterMapper registerMapper;

    @Override
    public void register(String userName, String encryptedPassword) throws NoSuchAlgorithmException {
        // 使用SHA-256+salt加密
        String salt = String.valueOf((int) (Math.random() * 1000000));
        //创建SHA-256加密对象
        MessageDigest messageDigest = MessageDigest.getInstance("SHA-256");
        //加盐加密
        messageDigest.update((encryptedPassword + salt).getBytes());
        //计算哈希值
        byte[] result = messageDigest.digest();
        //转换为十六进制字符串
        String encryptedPassword1 = new HexBinaryAdapter().marshal(result);
        Admin admin = new Admin();
        admin.setUserName(userName);
        admin.setEncryptedPassword(encryptedPassword1.toLowerCase());
        admin.setSalt(salt);
        registerMapper.register(admin);
    }

    //    @Autowired
//    private PasswordEncoder passwordEncoder;
    //注册逻辑

}
